package com.bestcem.xm.survey.config;

import com.github.benmanes.caffeine.cache.Cache;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @ClassName: RunnerLoadData
 * @Description:
 * @Author: chenglong.yue
 * @Date: 2022/10/13 17:26
 */
@Slf4j
@Component
public class RunnerLoadData implements CommandLineRunner {

    public RunnerLoadData(@NonNull Cache<String, Object> xmSurveyCache, SurveyWechatSetting surveyWechatSetting) {
        this.xmSurveyCache = xmSurveyCache;
        this.surveyWechatSetting = surveyWechatSetting;
    }

    @Qualifier("xmSurveyCache")
    private final Cache<String, Object> xmSurveyCache;

    private final SurveyWechatSetting surveyWechatSetting;

    @Override
    public void run(String... args) {
        initSurveyWechatSetting();
    }

    private void initSurveyWechatSetting() {
        log.info("微信认证相关配置加载到内存中");
        xmSurveyCache.put("redirect_uri", surveyWechatSetting.getRedirectUri());
        xmSurveyCache.put("component_appid", surveyWechatSetting.getComponentAppid());

        xmSurveyCache.put("appid", surveyWechatSetting.getAppid());
        xmSurveyCache.put("secret", surveyWechatSetting.getSecret());
        xmSurveyCache.put("access_token_url", surveyWechatSetting.getAccessTokenUrl());
        xmSurveyCache.put("oauth2_authorize", surveyWechatSetting.getOauth2Authorize());
    }
}
